/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import org.aswing.AbstractListCell;
import org.aswing.ASWingConstants;
import org.aswing.border.EmptyBorder;
import org.aswing.Component;
import org.aswing.geom.Point;
import org.aswing.Insets;
import org.aswing.JLabel;
import org.aswing.JList;
import org.aswing.JSharedToolTip;
import org.aswing.LoadIcon;

import fever.utils.DateUtils;
import fever.utils.Stringifier;

import fvaswing.components.reader.video.FvVideoMarker;

/**
 * Marker cell implementation for {@link fvaswing.components.reader.video.FvVideoBookmark} 
 * list.
 * 
 * @see fvaswing.components.reader.FvVideoPlayer
 * 
 * @author Romain Ecarnot
 */
class fvaswing.components.reader.video.FvMarkerCell extends AbstractListCell
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var sharedToolTip : JSharedToolTip;
	
	private var _view : JLabel;
	private var _value : FvVideoMarker;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function FvMarkerCell( )
	{
		super();
		
		if( sharedToolTip == null )
		{
			sharedToolTip = new JSharedToolTip();
			sharedToolTip.setOffsetsRelatedToMouse( false );
			sharedToolTip.setOffsets( new Point( 0, 0 ) );
		}
	}
	
	/**
	 * Sets the value of this cell.
	 *  
	 * @param value which should represent on the component of this cell. 
	 */
	public function setCellValue( value : FvVideoMarker ) : Void
	{
		_value = value;
		_getLabel().setText( value.name + "\n" + DateUtils.seconde2time( value.time ) );
		
		if( value.previewPath ) _getLabel().setIcon( new LoadIcon( value.previewPath ) );
		else _getLabel().setIcon( null );
		
		_getLabel().revalidateIfNecessary();
		
		_resized();
	}
	
	/**
	 * Returns the value of the cell.
	 */
	public function getCellValue() : FvVideoMarker
	{
		return _value;
	}
	
	/**
	 * Returns concrete cell component.
	 * 
	 * <p>Overrides method to keep strong typing with your 
	 * own component.
	 */
	public function getCellComponent() : Component
	{
		return _getLabel();
	}
	
	/**
	 * Sets the list cell status, include the owner-JList isSelected, index position.
	 * 
	 * @param the cell's owner, a JList
	 * @param isSelected true to set the cell selected, false to set not selected.
	 * @param index the row position
	 */
	public function setListCellStatus( component : JList, isSelected : Boolean, index : Number ) : Void
	{
		if( isSelected )
		{
			_view.setBackground( component.getSelectionBackground() );
			_view.setForeground( component.getSelectionForeground() );
		}
		else
		{
			_view.setBackground( component.getBackground() );			_view.setForeground( component.getForeground() );
		}
		
		_view.setFont( component.getFont() );
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _getLabel() : JLabel
	{
		if( _view == null)
		{
			_view = new JLabel();
			_view.setHorizontalAlignment( JLabel.LEFT );			_view.setHorizontalTextPosition( ASWingConstants.RIGHT );
			_view.setOpaque( true );
			_view.setFocusable( false );
			_view.setBorder( new EmptyBorder( null, new Insets( 5,5,5,5 ) ) );
			_view.addEventListener( JLabel.ON_RESIZED, _resized, this );
		}
		return _view;
	}
	
	private function _resized() : Void
	{
		if( _getLabel().getWidth() < _getLabel().getPreferredWidth() )
		{
			_getLabel().setToolTipText(value.toString());
			JSharedToolTip.getSharedInstance().unregisterComponent(_getLabel());
			sharedToolTip.registerComponent(_getLabel());
		}
		else
		{
			_getLabel().setToolTipText( null );
			sharedToolTip.unregisterComponent( _getLabel() );
		}
	}
}